package base.web.actions.base.login;

import base.service.bo.base.user.IBoBase_Ac_User;
import base.service.domain.exception.DaoException;
import base.service.domain.exception.NoSuchBeanException;
import base.web.actions.BaseAction;
import base.web.actions.base.model.BaseModelAction;
import base.web.tools.BeansHelp;
import base.web.tools.JsonHelp;
import base.web.tools.WebUtils;
import com.sun.org.apache.regexp.internal.RE;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by IntelliJ IDEA.
 * User: YeQing
 * Date: 12-7-9
 * Time: 上午10:20
 * To change this template use File | Settings | File Templates.
 */
public class LoginAction  extends BaseAction {

    public String index() {
        return getResult();
    }


    public String login() {
        String returnString = "success";
        try {



            String RETURN_URL = "/index.do";
            String theme = "";
            HttpServletRequest request = WebUtils.getRequest();
            HttpServletResponse response = WebUtils.getResponse();
            Cookie userCookie = WebUtils.getCookie(request ,"userID");
            if(userCookie == null) {
                mapParam.clear();
                mapParam = WebUtils.getParameterMap();
                Map paramMap = new HashMap();
                paramMap.put("usercode", mapParam.get("username"));
                IBoBase_Ac_User iboUser = (IBoBase_Ac_User) BeansHelp.getBeanInstance("boBase_Ac_User");
                List list = iboUser.getListBase_Ac_User(paramMap);
                if(list.size() == 0) {
                    throw new DaoException("User Not Exits.");
                }
                Map userMap = (Map) list.get(0);

                if(!mapParam.get("password").equals(userMap.get("userpassword"))) {
                    throw new DaoException("Password Error.");
                }

                Cookie cookie = new Cookie("userID", String.valueOf(userMap.get("userid")));
                response.addCookie(cookie);
                model.put("returnUrl", RETURN_URL);
            }else {
                returnString = "dispatcher";
            }

        } catch (Exception e) {
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
            this.code = -1;
			this.msg = e.getMessage();
			expMsg = getExceptionMessage(e);
			log.error(e.getMessage());
        }


        return returnString;
    }

}
